import os
import unittest
from unittest.mock import MagicMock

from hackingBuddyGPT.usecases.web_api_testing.documentation.parsing import OpenAPISpecificationParser
from hackingBuddyGPT.utils.prompt_generation.information import PenTestingInformation
from hackingBuddyGPT.usecases.web_api_testing.utils.configuration_handler import ConfigurationHandler


class TestPenTestingInformation(unittest.TestCase):

    def setUp(self):
        self.response_handler = MagicMock()
        self.config_path = os.path.join(os.path.dirname(__file__), "test_files","fakeapi_config.json")
        self.configuration_handler = ConfigurationHandler(self.config_path)
        self.config = self.configuration_handler._load_config(self.config_path)
        self._openapi_specification_parser = OpenAPISpecificationParser(self.config_path)
        self._openapi_specification = self._openapi_specification_parser.api_data



    def test_assign_endpoint_categories(self):
        self.pentesting_information = self.generate_pentesting_information("icl")

        creation_paths = [ep.get("path") for ep in self.pentesting_information.categorized_endpoints.get("account_creation")]
        protected_endpoints = [ep.get("path") for ep in  self.pentesting_information.categorized_endpoints.get("protected_endpoint")]

        self.assertIn('/users', creation_paths)
        self.assertIn('/users/{id}',protected_endpoints)


    def test_key_in_path(self):
        self.pentesting_information = self.generate_pentesting_information("icl")
        self.pentesting_information.resources = {"user": ["1", "2"]}
        found, key = self.pentesting_information.key_in_path("/api/v1/user/1", self.pentesting_information.resources)
        self.assertTrue(found)
        self.assertEqual(key, "user")

    def test_generate_authentication_prompts(self):
        self.pentesting_information = self.generate_pentesting_information("icl")
        result = self.pentesting_information.generate_authentication_prompts()
        self.assertIsInstance(result, list)
        self.assertEqual(len(result), 0)

    def test_generate_input_validation_prompts(self):
        self.pentesting_information = self.generate_pentesting_information("icl")
        result = self.pentesting_information.generate_input_validation_prompts()
        self.assertIsInstance(result, list)
        self.assertEqual(len(result), 0)

    def test_generate_authorization_prompts(self):
        self.pentesting_information = self.generate_pentesting_information("icl")

        result = self.pentesting_information.generate_authorization_prompts()
        self.assertIsInstance(result, list)
        self.assertEqual(len(result), 0)

    def test_generate_error_handling_prompts(self):
        self.pentesting_information = self.generate_pentesting_information("icl")

        result = self.pentesting_information.generate_error_handling_prompts()
        self.assertIsInstance(result, list)
        self.assertEqual(len(result), 0)

    def test_generate_session_management_prompts(self):
        self.pentesting_information = self.generate_pentesting_information("icl")

        result = self.pentesting_information.generate_session_management_prompts()
        self.assertIsInstance(result, list)
        self.assertEqual(len(result), 0)

    def test_generate_xss_prompts(self):
        self.pentesting_information = self.generate_pentesting_information("icl")

        result = self.pentesting_information.generate_xss_prompts()
        self.assertIsInstance(result, list)
        self.assertEqual(len(result), 0)

    def generate_pentesting_information(self, param):
        config, strategy = self.configuration_handler.load(param)
        self.pentesting_information = PenTestingInformation(self._openapi_specification_parser, config)
        return self.pentesting_information


if __name__ == '__main__':
    unittest.main()
